package com.yh.modules.data.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.common.annotation.DataFilter;
import com.yh.common.constant.Constant;
import com.yh.common.constant.StatusEnums;
import com.yh.common.exception.RRException;
import com.yh.common.utils.DataFilterUtil;
import com.yh.common.utils.PageUtils;
import com.yh.common.utils.Query;
import com.yh.modules.data.dao.MachineDao;
import com.yh.modules.data.entity.MachineEntity;
import com.yh.modules.data.entity.ProjectEntity;
import com.yh.modules.data.service.MachineService;
import com.yh.modules.data.service.ProjectService;
import com.yh.modules.sys.entity.SysDeptEntity;
import com.yh.modules.sys.service.SysDeptService;
import com.yh.shiro.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("machineService")
@Slf4j
public class MachineServiceImpl extends ServiceImpl<MachineDao, MachineEntity> implements MachineService {

	@Autowired
	private ProjectService projectService;

	@Autowired
	private SysDeptService sysDeptService;

	@Override
	@DataFilter(subDept = true, user = false)
	public PageUtils queryPage(Map<String, Object> params) {
		Integer projectId = MapUtil.getInt(params, "projectId");
		String machineName = MapUtil.getStr(params, "machineName");
		Integer deptId = DataFilterUtil.getDeptId(params);
//		log.info("f: {}", MapUtil.getStr(params, Constant.SQL_FILTER));

		EntityWrapper<MachineEntity> wrapper = new EntityWrapper<>();
		wrapper.like(StrUtil.isNotBlank(machineName), "name", machineName);
		wrapper.eq(ObjectUtil.isNotNull(projectId), "project_id", projectId);
		wrapper.eq(ObjectUtil.isNotNull(deptId), "dept_id", deptId);
		wrapper.addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER));

		Page<MachineEntity> page = this.selectPage(new Query<MachineEntity>(params).getPage(), wrapper);

		List<MachineEntity> records = page.getRecords();
		for (MachineEntity machineEntity : records) {
			//设置项目名称
			setProjectName(machineEntity);

			//设置部门名称
			setDeptName(machineEntity);
		}

		return new PageUtils(page);
	}



	private void setDeptName(MachineEntity machineEntity) {
		SysDeptEntity sysDeptEntity = sysDeptService.selectById(machineEntity.getDeptId());
		if (sysDeptEntity != null) {
			machineEntity.setDeptName(sysDeptEntity.getName());
		} else {
			machineEntity.setDeptName("");
		}
	}

	private void setProjectName(MachineEntity machineEntity) {
		ProjectEntity projectEntity = projectService.selectById(machineEntity.getProjectId());
		if (projectEntity != null) {
			machineEntity.setProjectName(projectEntity.getName());
		} else {
			machineEntity.setProjectName("");
		}
	}

	@Override
	public List<MachineEntity> getMachineListByDeptId(Long deptId) {
		EntityWrapper<MachineEntity> wrapper = new EntityWrapper<>();
		wrapper.eq("dept_id", deptId);
		return this.selectList(wrapper);
	}

	@Override
	public Integer getMachineCountByDeptId(Long deptId) {
		List<MachineEntity> machineList = getMachineListByDeptId(deptId);

		return CollUtil.isNotEmpty(machineList) ? machineList.size() : 0;
	}

	@Override
	public void save(MachineEntity machine) {
		//校验机器名称是否重复
		checkMachineNameRepeat(machine);

		machine.setStatus(StatusEnums.ENABLE.getCode());
		machine.setCreateTime(new Date());
		machine.setUpdateTime(new Date());
		this.insert(machine);
	}

	@Override
	public void update(MachineEntity machine) {
		checkMachineNameRepeat(machine);
		machine.setUpdateTime(new Date());
		this.updateById(machine);
	}

	private void checkMachineNameRepeat(MachineEntity machine) {
		EntityWrapper<MachineEntity> wrapper = new EntityWrapper<>();
		wrapper.eq("project_id", machine.getProjectId());
		wrapper.eq("name", machine.getName());
		wrapper.ne(ObjectUtil.isNotNull(machine.getId()), "id", machine.getId());
		int count = this.selectCount(wrapper);
		if (count > 0) {
			throw new RRException("同个项目下机器名称不能重复");
		}
	}

}
